<!--选择题-->
<template>
  <div>
      <div style="margin-bottom: 20px;">
        <el-cascader
          placeholder="请选择"
          :options="options"
          filterable
          size="mini"
          v-model="dataForm.f_sb_id"
        ></el-cascader>
        <el-select v-model="dataForm.f_ch_type" placeholder="类型" size="mini">
          <el-option label="单选题" value="1"></el-option>
          <el-option label="多选题" value="2"></el-option>
        </el-select>
        <el-button size="mini" @click="resetInfo">重置</el-button>
        <el-button size="mini" type="success" icon="el-icon-search" @click="searchSelectByType">查询</el-button>
        <el-button size="mini" type="primary" icon="el-icon-circle-plus" @click="addSelect">添加</el-button>
        <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteSelect">删除</el-button>
      </div>
    <el-table
      :data="tableData"
      stripe
      @selection-change="handleSelectionChange">
    style="width: 100%">
      <el-table-column
        type="selection"
        width="80">
      </el-table-column>
      <el-table-column
        type="index"
        width="80">
      </el-table-column>
      <el-table-column
        label="课目"
        prop="sb_name"
        width="150">
      </el-table-column>
      <el-table-column
        prop="ch_title"
        label="题干"
        minWidth="180">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleClick(scope.row)" >查看</el-button>
          <el-button  size="mini" @click="handleDelete(scope.row)" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="pageInfo.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total">
    </el-pagination>
    <!--添加选择器-->
    <add-select v-if="addShow" :addShow="addShow" @closeDialog="closeDialog"></add-select>
    <!--编辑选择题-->
    <edit-select v-if="editShow" :editShow="editShow" :selectedData="selectedData" @closeDialog="closeDialog"></edit-select>
  </div>
</template>
<script>
  import {addChoose,deleteChoose,updateChoose,findChoose,getAllSubject} from '@/api/questions'
  import addSelect from './question/addSelect.vue'
  import editSelect from './question/editSelect.vue'
  import ElButton from "../../../../node_modules/element-ui/packages/button/src/button";
  export default{
      components:{
        ElButton,
        addSelect,
        editSelect
      },
    data(){
      return {
        tableData:[],
        multipleSelection:[], //复选框选中的数据
        addShow:false,
        editShow:false,
        selectedData:null,
        options:[], //选择所属科目
        dataForm:{
          f_sb_id:[],
          f_ch_type:''
        },

        pageInfo:{
          page:1,//页数
          size:10, //页面大小
          total:0 //页总数
        },
        requestData:{
          pageSize:10,
          pageNum:1,
          map:{
            sb_id:0,
            ch_type:0
          }
        }
      }
    },
    mounted(){
      this.c_getAllSubject();
      this.searchSelect();
    },
    methods:{
      searchSelect(){
        this.multipleSelection = [];
        findChoose(this.requestData).then(res=>{
          if(res.code == 200){
            this.tableData = res.data.data;
            this.pageInfo.total = res.data.total;
          }
        })
      },

      searchSelectByType(){
        //对一些特殊数据进行重新封装
        if(this.dataForm.f_sb_id.length == 3 && this.dataForm.f_ch_type != ''){
          this.requestData.map.sb_id = parseInt(this.dataForm.f_sb_id[2]); //所属科目
          this.requestData.map.ch_type = parseInt(this.dataForm.f_ch_type);

          this.pageInfo.page = 1;
          this.pageInfo.size = 10;
          this.pageInfo.total = 0;

          this.requestData.pageNum = 1;
          this.requestData.pageSize = 10;

          this.searchSelect();
        }
      },

      //重置
      resetInfo(){
        this.dataForm = {
          f_sb_id:[],
          f_ch_type:''
        };

       this.requestData .map.sb_id = 0;
       this.requestData .map.ch_type = 0;

        this.requestData.pageNum = 1;
        this.pageInfo.page = 1;

        this.c_getAllSubject();
        this.searchSelect();
      },

      addSelect(){
        this.addShow = true
      },

      //批量删除
      deleteSelect(){
         if(this.multipleSelection.length > 0){
           this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
             confirmButtonText: '确定',
             cancelButtonText: '取消',
             type: 'warning'
           }).then(() => {
             deleteChoose(this.multipleSelection).then(res=>{
               if(res.code == 200){
                 this.$message({
                   type: 'success',
                   message: '删除成功'
                 });
                 this.searchSelect();
               }else{
                 this.$message({
                   type: 'error',
                   message: '删除失败'
                 });
               }
             })
           }).catch(() => {
           });
         }else{
           this.$message({
             type: 'warning',
             message: '请至少选择一条数据'
           });
         }
      },

      //编辑
      handleClick(row){
         this.selectedData = row;
         this.editShow = true;
      },

      //单个删除
      handleDelete(row){
          let chooseList = [];
          chooseList.push(row);

        this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteChoose(chooseList).then(res=>{
            if(res.code == 200){
              this.$message({
                type: 'success',
                message: '删除成功'
              });
              this.searchSelect();
            }else{
              this.$message({
                type: 'error',
                message: '删除失败'
              });
            }
          })
        }).catch(() => {

        });

      },

      //点击复选框时触发
      handleSelectionChange(val){
        this.multipleSelection = val;
      },

      closeDialog(type){
          if(type == 1){
              this.addShow = false;
          }else if(type == 2){
              this.editShow = false;
          }
        this.searchSelect();
      },

      //每页的条数发生改变时调用
      handleSizeChange(size){
        this.requestData.pageSize = size;
        this.pageInfo.size = size;
        this.searchSelect();
      },

      //页数发生改变时调用
      handleCurrentChange(page){
        this.requestData.pageNum = page;
        this.pageInfo.page = page;
        this.searchSelect();
      },

      //获取所有级联选项
      c_getAllSubject(){
        this.dataForm.f_sb_id = [];
        getAllSubject().then(res=>{
          if(res.code == 200){
            for(let i = 0;i < res.data.length;i ++){
              let tLevel = {value:res.data[i].l_id,label:res.data[i].l_name};
              this.options.push(tLevel);
              if(res.data[i].list != null && res.data[i].list.length > 0){
                let gList = res.data[i].list;
                let children = [];
                for(let j = 0;j < gList.length;j ++){
                  let gGroup = {value:gList[j].g_id,label:gList[j].g_name};
                  children.push(gGroup);

                  if(gList[j].list != null && gList[j].list.length > 0){
                    let sList = gList[j].list;
                    let children1 = [];

                    for(let n = 0;n < sList.length;n ++){
                      let subject = {value:sList[n].sb_id,label:sList[n].sb_name};
                      children1.push(subject);
                    }
                    gGroup.children = children1;
                  }
                }
                tLevel.children = children;
              }
            }
          }
        });
      },
    }
  }
</script>
